import BaseView from "../../../cfw/mvc/BaseView";
import ClotheIconView from "./ClotheIconView";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import DecorationControllerInterface from "../../../public/interface/DecorationControllerInterface";
import DecorationMgrInterface from "../../../public/interface/DecorationMgrInterface";
import ItemView from "../../item/ItemView";
import BaseItemModel from "../../../cfw/mvc/BaseItemModel";
import GridLayerListView from "../../../engine/GridLayerListView";


const { ccclass, property } = cc._decorator;

@ccclass
export default class DressingRoomView extends BaseView {

    @property(GridLayerListView)
    leftScrollView: GridLayerListView= null;

    @property(GridLayerListView)
    rightScrollView: GridLayerListView = null;

    // @property(cc.Prefab)
    // leftIconPrefab: cc.Prefab = null;

    // @property(cc.Prefab)
    // rightItemPrefab: cc.Prefab = null;

    protected controller: DecorationControllerInterface;

    protected model: DecorationMgrInterface;


  
    protected clothes: BaseItemModel[]
    start() {
        super.start()
        let columnList = this.model.getColumnList()
        GlobalEvent.instance().on(EventName.SELECT_COLUMN, this.onButtonColumnClick, this)
        this.leftScrollView.totalCount = columnList.length
        this.clothes = this.model.getContentList(columnList[0].getID())
        this.rightScrollView.totalCount = this.clothes.length;

    }

    onDestroy() {
        super.onDestroy();
        GlobalEvent.instance().off(EventName.SELECT_COLUMN, this.onButtonColumnClick, this)
    }


    leftListViewUpdate(listView, pos: number, item: cc.Node) {
        let columnList = this.model.getColumnList()
        let comp: ClotheIconView = this.controller.initView(item, 'ClotheIconView', columnList[pos]) as ClotheIconView
        if (comp.isLoad()) {
            comp.refresh()
        } else {
            // cc.log(' pos == ',pos)
            // if (pos == 0) {
            //     this.onButtonColumnClick(columnList[pos].getID())
            // }
        }
    }

    rightListViewUpdate(listView, pos: number, item: cc.Node) {
        // let columnList = this.model.getColumnList()
        // cc.log(" rightListViewUpdate ",pos)
        let comp: ItemView = this.controller.initView(item, 'ItemView', this.clothes[pos]) as ItemView
        if (comp.isLoad()) {
            comp.refresh()
        }
    }


    onButtonColumnClick(columnID: number) {
        this.clothes = this.model.getContentList(columnID)
        if (this.rightScrollView) {
            this.rightScrollView.reset(this.clothes.length)
        }
    }


    onButtonBackClick() {
        this.controller.back()
    }


}
